package  service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import  dao.inter.IAccountDao;
import  entity.Account;
import  service.inter.IAccountService;
import  tool.MD5Util;

@Service
public class AccountService implements IAccountService {
@Autowired
private IAccountDao iAccountDao;
private Logger logger =Logger.getLogger(AccountService.class);
	public Account save(Account account){
		return account=iAccountDao.save(account);
	}
public boolean delete(String rowid){
	return iAccountDao.delete(rowid);
}
public boolean update(Account account){
	return iAccountDao.update(account);
}
public Account queryById(String rowid){
	return iAccountDao.queryById(rowid);

}
public Account queryByAccount(String account){
	if(account==null||account.isEmpty()) return null;
	StringBuffer sql=new StringBuffer();
	sql.append("select * from account where account='").append(account).append("'");
	List<Account>accountList= iAccountDao.queryBySql(sql.toString());
	if(accountList!=null&&accountList.size()!=0){
		Account entity=accountList.get(0);
		return entity;
	}else{
		return null;
	}
}
public boolean verifyAccount(Account accountInDB,Account accountAccepted){
	if(accountAccepted.getCode()==null) return false;
	String pwDB=accountInDB.getCode();
	String pwAA=MD5Util.MD5(accountAccepted.getCode());
	if(pwAA.equals(pwDB)) return true;
	else return false;
}
public Map login(Account account,
			HttpServletRequest request,HttpServletResponse response){
	Map map=new HashMap<String,Object>();
	Account returnAccount=null;
	StringBuffer info=new StringBuffer();
	if(account.getAccount()!=null){
		info.append(account.getAccount()+",");
	}
	boolean success=false;
	returnAccount=this.queryByAccount(account.getAccount());
	if(returnAccount==null){
		info.append("���˺�δע��") ;
	}else if(!verifyAccount(returnAccount, account)){
		info.append("�������") ;
	}else{
		info.append("��¼�ɹ�") ;
		success=true;
		//����¼��Ϣ���浽cookie��
//		String userInfo=returnAccount.getAccount()+";"+returnAccount.getCode();
//		byte[] ssid=null;
//		try {
//			ssid = DESUtil.encrypt(userInfo.getBytes("utf-8"), DESUtil.getPassword());
//			Cookie cookie=new Cookie("ssid", URLEncoder.encode(new String(ssid,"utf-8"), "utf-8"));
//			response.addCookie(cookie);
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
	logger.info(info);
	map.put("info", info.toString());
	map.put("success", success);
//	map.put("entity", jsonEntity);
	map.put("entity", returnAccount);
	return map;
}
public Map register(Account account,
		HttpServletRequest request,HttpServletResponse response){
	Map map=new HashMap<String,Object>();
	StringBuffer info=new StringBuffer();
	Account realAccount=null;
	boolean success=false;
	//�˺���Ϣ����
	if(StringUtils.isEmpty(account.getAccount())||StringUtils.isEmpty(account.getCode())){
		info.append("����д��ע����Ϣ");
	}else{
		realAccount=this.queryByAccount(account.getAccount());
		if(realAccount==null){
			String password=account.getCode();
			account.setCode(MD5Util.MD5(password));
			account.setRowid(null);
			account=iAccountDao.save(account);
			if(account!=null){
				info.append("ע��ɹ�");
//				jsonEntity=JsonUtil.getJson(account);
				success=true;
			}else{
				info.append("ע��ʧ��");
			}
		}else{
			info.append("���˺��ѱ�ע��") ;
		}
	}
	
	logger.info(info);
	map.put("info", info.toString());
	map.put("success", success);
//	map.put("entity", jsonEntity);
	map.put("entity", account);
	return map;
}
public Map updateAccount(Account account,
		HttpServletRequest request,HttpServletResponse response){
	Map map=new HashMap<String,Object>();
	StringBuffer info=new StringBuffer();
//	String jsonEntity="";
	Account realAccount=null;
	boolean success=false;
	realAccount=this.queryById(account.getRowid());
	if(realAccount!=null){
		//����������Ϣ
		realAccount.setName(account.getName());
		realAccount.setPhone(account.getPhone());
		realAccount.setSex(account.getSex());
		realAccount.setImg("images/"+account.getImg());
		iAccountDao.update(realAccount);
		if(realAccount!=null){
			info.append("���³ɹ�");
//			jsonEntity=JsonUtil.getJson(account);
			success=true;
		}else{
			info.append("����ʧ��");
		}
	}else{
		info.append("���˺Ż�δע��") ;
	}
	logger.info(info);
	map.put("info", info.toString());
	map.put("success", success);
//	map.put("entity", jsonEntity);
	map.put("entity", realAccount);
	return map;
}
@Override
public Map getList(HttpServletRequest request) {
	Map map=new HashMap<String,Object>();
	StringBuffer info=new StringBuffer();
	List<Account> entity=null;
	boolean success=false;
	StringBuffer hql=new StringBuffer();
	hql.append("from Account");
	entity=iAccountDao.queryByHql(hql.toString(),null);
	if(entity!=null&&entity.size()!=0){
			info.append("��ѯ�ɹ�");
//			jsonEntity=JsonUtil.getJson(account);
			success=true;
	}else{
		info.append("��ѯʧ��");
	}
	logger.info(info);
	map.put("info", info.toString());
	map.put("success", success);
	map.put("entity", entity);
	return map;
}
}
